package com.ldk.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ldk.base.framework.util.SystemUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ldk.admin.common.LigerUIGridResult;
import com.ldk.admin.common.ResponseResult;
import com.ldk.admin.constants.FORM;
import com.ldk.admin.constants.ViewMode;
import com.ldk.base.framework.common.CustomPageable;
import com.ldk.framework.entity.Driver;
import com.ldk.framework.entity.DriverCarImgs;
import com.ldk.framework.service.IDriverCarImgsService;
import com.ldk.framework.service.IDriverService;


/**
 * 司机车图片控制器
 * @author xiangwl
 * 2017年2月21日
 * com.ldk.admin.controller
 * DriverCarImgsController
 */
@Controller
@RequestMapping("/carimgs")
public class DriverCarImgsController extends BaseController {
	/**
	 * 路由地址
	 */
	private final String route = "carimgs";
	/**
	 * 模块名称
	 */
	private final String module = "车图片";
	
	@Autowired
	private IDriverService driverService;

	@Autowired
	private IDriverCarImgsService driverCarImgsService;
	
	@RequestMapping("/list/{openId}")
	//public ModelAndView list(@PathVariable(value="openId") String openId, ModelMap map){
	public ModelAndView list(@PathVariable(value="openId") String openId, ModelMap map,
							 HttpServletRequest request,
							 HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		if (this.getRoleId(request) == 6) {
			setLinks(map, "司机管理", "/driver/list", module+"管理", "/"+route+"/list/"+openId, "", "");
		} else {
			setLinks(map, "司机管理", "/driver/list", module+"管理", "/"+route+"/list/"+openId, "添加"+module, "/"+route+"/add/"+openId);
		}
		map.addAttribute("openId", openId);
		map.addAttribute("route", route);
		map.addAttribute("module", module);
		map.addAttribute("driver", getDriver(openId));

		//操作日志
		//this.insertOplog(request,module,"","","查看",this.getAdminUserName(request).toString(), Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());

		if (this.getRoleId(request) == 6) {
			return new ModelAndView("/carimgs/list_sj");
		} else {
			return new ModelAndView("/carimgs/list");
		}
	}
	
	@ResponseBody
	@RequestMapping("/getList")
	public LigerUIGridResult<DriverCarImgs> getList(@RequestParam(value="page",required=false,defaultValue="1") int page, 
									       @RequestParam(value="pagesize",required=false,defaultValue="10") int pageSize,
									       @RequestParam(value="openId",required=false,defaultValue="") String openId,
									       HttpServletRequest request)
	{
		int limit = pageSize;
		int offset = (page -1) * pageSize;
		
		DriverCarImgs query = new DriverCarImgs();
		query.setOpenId(openId);
		CustomPageable pageable = new CustomPageable(offset, limit);
		
		Page<DriverCarImgs> result = driverCarImgsService.selectPageList("", query, pageable);
		
		LigerUIGridResult<DriverCarImgs> grid = new LigerUIGridResult<>();
		grid.Total = result.getTotalElements();
		grid.Rows = result.getContent();
		
		return grid;
	}
	
	@RequestMapping("/add/{openId}")
	public ModelAndView add(@PathVariable(value="openId") String openId, ModelMap map,HttpServletRequest request,
							HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		setLinks(map, module+"管理", "/"+route+"/list/"+openId, "添加"+module, "/"+route+"/add/"+openId, "", "");
		setForm(map, "/"+route+"/insert", null);
		map.addAttribute("module", module);
		
		DriverCarImgs model = new DriverCarImgs();
		model.setOpenId(openId);
		
		return new ModelAndView("/carimgs/info","model",model);
	}
	
	@RequestMapping("/edit/{openId}/{id}")
	public ModelAndView edit(@PathVariable(value="openId") String openId,
							 @PathVariable(value="id") long id, ModelMap map,HttpServletRequest request,
							 HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		DriverCarImgs query = new DriverCarImgs();
		query.setImgId(id);
		query.setOpenId(openId);
		
		DriverCarImgs model = driverCarImgsService.selectByKey("", query);
		if(model == null){
			return new ModelAndView("redirect:/"+route+"/list/"+openId);
		}
		setLinks(map, module+"管理", "/"+route+"/list/"+openId, "编辑"+module, "/"+route+"/edit/"+openId+"/"+id, "", "");
		setForm(map, "/"+route+"/update", null);
		map.addAttribute("module", module);
		
		return new ModelAndView("/carimgs/info","model", model);
	}
	
	@RequestMapping("/insert")
	public ModelAndView insert(@ModelAttribute("model") DriverCarImgs model,
							   RedirectAttributes redAttr, ModelMap map, HttpServletRequest request,
							   HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		ResponseResult rst = new ResponseResult();
		redAttr.addAttribute("openId", model.getOpenId());
		redAttr.addFlashAttribute(FORM.FROM_RESULT, rst);
		
		try{
			
			if(model.getEnabled() == null){
				model.setEnabled(false);
			}

			//操作日志
			this.insertOplog(request,module,"","","添加:"+model.getImgId(),this.getAdminUserName(request).toString(),
					Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());

		    int row = driverCarImgsService.insert("",model);
		    if(row > 0){
		    	rst.AddOk("添加"+module+"成功");
		    }
		    else{
		    	rst.AddError("添加"+module+"失败");
		    }
		}
		catch(Exception e){
			rst.AddError("添加"+module+"失败");
			logger.error(e.getMessage());
		}
		return new ModelAndView("redirect:/"+route+"/add/{openId}");
	}
	
	@RequestMapping("/update")
	public ModelAndView update(@ModelAttribute("model") DriverCarImgs model,
			   				   RedirectAttributes redAttr, ModelMap map, HttpServletRequest request,
							   HttpServletResponse response)
	{
		//session是否过期
		this.isExpired(request,response);
		ResponseResult rst = new ResponseResult();

		redAttr.addAttribute("id", model.getImgId());
		redAttr.addAttribute("openId", model.getOpenId());
		redAttr.addFlashAttribute(FORM.FROM_RESULT, rst);
		
		try{
			
			if(model.getEnabled() == null){
				model.setEnabled(false);
			}
			//操作日志
			this.insertOplog(request,module,"","","修改:"+model.getImgId(),this.getAdminUserName(request).toString(),
					Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());

			int row = driverCarImgsService.updateByKey("", model);
		    if(row > 0){
		    	rst.AddOk("修改"+module+"成功");
		    }
		    else{
		    	rst.AddError("修改"+module+"失败");
		    }
		}
		catch(Exception e){
			rst.AddError("修改"+module+"失败");
			logger.error(e.getMessage());
		}
		
		return new ModelAndView("redirect:/"+route+"/edit/{openId}/{id}");
	}
	
	/**
	 * 浏览
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping("/view/{id}")
	public ModelAndView view(@PathVariable long id, ModelMap map,HttpServletRequest request,
							 HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		setViewMode(map, ViewMode.View);
		DriverCarImgs query = new DriverCarImgs();
		query.setImgId(id);
		DriverCarImgs model = driverCarImgsService.selectByKey("", query);
		
		map.addAttribute("module", module);
		
		return new ModelAndView("/carimgs/info","model", model);
	}
	
	@ResponseBody
	@RequestMapping("/remove/{id}")
	public ResponseResult remove(@PathVariable long id, HttpServletRequest request){
		
		ResponseResult rst = new ResponseResult();
		if(id < 1)
		{
			rst.AddError("删除"+module+"错误");
			return rst;
		}
		Boolean successed = false;
		try{
			DriverCarImgs query = new DriverCarImgs();
			query.setImgId(id);
			DriverCarImgs driverCarImgs = driverCarImgsService.selectByKey("", query);
			//操作日志
			this.insertOplog(request,module,"","","删除:"+driverCarImgs.getImgUrl(),this.getAdminUserName(request).toString(),
					Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());


			int row = driverCarImgsService.deleteByKey("", query);
			successed = row > 0;
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		if(successed){
			rst.AddOk("删除"+module+"成功");
		}
		else{
			rst.AddError("删除"+module+"失败");
		}
		
		return rst;
	}
	
	private Driver getDriver(String openId){
		Driver query = new Driver();
		query.setOpenId(openId);
		
		return driverService.selectOne("", query);
	}
}
